﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace HousingDistribution
{
    class HousingDisDeadline
    {

        public static HousingDisDeadline Instance()
        {
            if (instance == null)
                instance = new HousingDisDeadline();

            return instance;
        }

        public DateTime Deadline()
        {
            //return this.deadline;
            return ReadData();
        }

        public void Deadline(DateTime deadline)
        {
            this.deadline = deadline;

            Save(deadline);

        }

        public void Save(DateTime deadline)
        {
            string filePath = DEADLINE_FILE_PATH;

            XmlWriterSettings setting = new XmlWriterSettings();
            setting.Indent = true;
            setting.NewLineOnAttributes = true;

            using (XmlWriter writer = XmlWriter.Create(filePath, setting))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("elements");

                writer.WriteElementString("Deadline", deadline.ToShortDateString());

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
        }

        public DateTime ReadData()
        {
            string filePath = DEADLINE_FILE_PATH;

            try
            {
                using (XmlReader rdr = XmlReader.Create(filePath))
                {
                    while (rdr.Read())
                    {
                        if (rdr.MoveToContent() == XmlNodeType.Element && rdr.Name == "Deadline")
                        {
                            deadline = this.dateProc.Parse(rdr.ReadElementString());
                        }
                    }
                    rdr.Close();
                }

                return deadline;
            }
            catch (Exception e)
            {
                //发生异常
                System.Windows.Forms.MessageBox.Show(e.Message);
                return deadline;
            }
            //finally
            //{
            //    //创建一个默认文件
            //    Save(deadline);
            //}
        }

        private HousingDisDeadline()
        {
        }

        DateTime deadline = DateTime.Now;
        private static HousingDisDeadline instance = null;
        private string DEADLINE_FILE_PATH = ".\\Deadline.xml";
        DateProc dateProc = new DateProc("YY-MM-DD");

        //public DateTime Deadline()
        //{
        //    return this.deadline;
        //}

        //public void Deadline(DateTime deadline)
        //{
        //    this.deadline = deadline;
        //}

        //private HousingDisDeadline()
        //{
        //}

        //DateTime deadline = DateTime.Now;

        //static HousingDisDeadline instance;
    }
}
